//package com.wmlt.file;
//
//import java.io.IOException;
//import java.nio.file.Paths;
//import java.util.Arrays;
//import java.util.List;
//import java.util.stream.Collectors;
//
//import javax.servlet.http.HttpServletRequest;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.discovery.DiscoveryClient;
//import org.springframework.core.io.Resource;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.multipart.MultipartFile;
//
//import com.cgv.api.service.FileService;
//import com.cgv.api.web.rest.vm.FileDTO;
//import com.cgv.api.web.rest.vm.ResultData;
//
//
//
///**
// * create by huangziqideveloper@gmail.com
// */
//public class FileResource {
//	private  String appName;
//	private  String pathBase = "/img";
//	private static final String PATH_PRODUCT = "product";
//    private FileService fileService;
//    DiscoveryClient discoveryClient;
//
//    /**
//     * @param service 服务ID
//     **/
//	public List<ServiceInstance> serviceUrl(String service) {
//		List<ServiceInstance> instances = discoveryClient.getInstances(service);
//		return instances;
//	}
//
//    /**
//     * 下载文件
//     * @param fileName
//     * @param request
//     * @return
//     */
//	public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
//	    // Load file as Resource
//
//	    Resource resource = fileService.loadFileAsResource(fileName);
//
//	    // Try to determine file's content type
//	    String contentType = null;
//	    try {
//	        contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
//	    } catch (IOException ex) {
//	        log.info("Could not determine file type.");
//	    }
//
//	    // Fallback to the default content type if type could not be determined
//	    if(contentType == null) {
//	        contentType = "application/octet-stream";
//	    }
//
//	    return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType)).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"").body(resource);
//	}
//
//    /**
//	 * 上传单个文件
//	 * @param file
//	 * @return
//	 */
//	public ResultData<FileDTO> upload(@RequestParam("file") MultipartFile file){
//	    FileDTO fileDTO = uploadFile(file);
//	    return ResultData.ok(fileDTO);
//	}
//
//	/**
//     * 上传多个文件
//     * @param files
//     * @return
//     */
//	public ResultData<List<FileDTO>> uploadMultiple(@RequestParam("files") MultipartFile[] files) {
//	     List<FileDTO> fiels = Arrays.stream(files).map(this::uploadFile).collect(Collectors.toList());
//	     return ResultData.ok(fiels);
//	}
//
//    /**
//	 * 上传单个文件(product)
//	 * @param file
//	 * @param productId
//	 * @return
//	 */
//	public ResultData<FileDTO> upload(@RequestParam("file") MultipartFile file, @PathVariable("productId") String productId){
//	    FileDTO fileDTO = uploadFile(file, productId);
//	    return ResultData.ok(fileDTO);
//	}
//
//	/**
//     * 上传多个文件
//     * @param files
//     * @return
//     */
//	public ResultData<List<FileDTO>> uploadMultiple(@RequestParam("files") MultipartFile[] files, @PathVariable("productId") String productId) {
//		List<FileDTO> fiels =  Arrays.stream(files).map(item -> this.uploadFile(item, productId)).collect(Collectors.toList());
//		return ResultData.ok(fiels);
//	}
//
//	private FileDTO uploadFile(MultipartFile file) {
//		return this.uploadFile(file, null);
//	}
//
//	private FileDTO uploadFile(MultipartFile file, String productId) {
//		String path = productId != null ? PATH_PRODUCT+"/"+ String.valueOf(productId) : "";
//		String fileName = fileService.storeFile(file, path);
//		String fileDownloadUri = Paths.get(pathBase).resolve(path).resolve(fileName).toString();
//
//	    FileDTO fileDTO = new FileDTO(fileName, fileDownloadUri, fileService.getMimeType(file), file.getSize());
//	    log.debug(fileDownloadUri);
//		return fileDTO;
//	}
//
//
//	/**
//	 * 获取路径
//	 * @param fileName
//	 * @param path
//	 * @return
//	 */
//	private String buildPath(String fileName, String path) {
////		List<ServiceInstance> serviceUrl = this.serviceUrl("toctoc");
////		log.debug("serviceUrl:==>>{}", JsonUtil.bean2Json(serviceUrl));
////		String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
////		        .path("/" + appName + PATH_BASE)
////                .path(path)
////                .path("/"+fileName)
////                .toUriString();
//		StringBuilder fileDownloadUri = new StringBuilder();
//		fileDownloadUri.append(pathBase)
//						.append(path != null ? path : "")
//						.append("/" + fileName);
//
//		return fileDownloadUri.toString();
//	}
//}
